package com.wifiscanner;


//import MarkerOverlay;

import java.util.ArrayList;
import java.util.List;


import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.wifiscanner.DBAdapter.AccessPointCursor;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;

public class MapViewActivity extends MapActivity {

	DBAdapter db;
	//private List<Overlay> overlays;
	//private MarkerOverlay marker_layer;
	//private MapController m_controller;
	AccessPointItemizedOverlay apOverlay;
	
	private static final int SIGNAL_GOOD = -65;	// strength for a good signal
	private static final int SIGNAL_OK = -85;
	
	
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.mapview_layout);
        
        db = new DBAdapter(this);
	    db.open();
	        
        MapView mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);    
        
	    List<Overlay> mapOverlays = mapView.getOverlays();
	    
		Drawable drawable = this.getResources().getDrawable(R.drawable.access_point);    
	    // load all known access points
		AccessPointCursor cursor = db.getAccessPoints(null,null, "lat,lon");
 		try {	
 			cursor.moveToFirst();
 		    int prev_lat = 0;
 		    int prev_lon = 0;
 		    int num_same = 0;
 			do {
 				
 				AccessPoint point= cursor.getAccessPoint();
 				if (!point.isZeroFix()) {
 					int lat = (int) (point.lat* 1E6);
 					int lon = (int) (point.lon * 1E6);
 					
 					// check if point has same co-ords as previous
 					if (lat == prev_lat && lon == prev_lon) {	
 						num_same++;
 					}
 					else {
 						prev_lon = lon;
 						prev_lat = lat;
 						num_same = 0;
 					}
 					String title = point.SSID;
 					String info = "MAC:\t" + point.BSSID +'\n';
 							info +="Power:\t" + point.level + "dBm" + '\n';
 							
 					if (point.level >  SIGNAL_GOOD){
 						drawable = this.getResources().getDrawable(R.drawable.green_light);
 						
 					}else if (point.level >  SIGNAL_OK){
 						drawable = this.getResources().getDrawable(R.drawable.orange_light);
 					}
 					else{
 						drawable = this.getResources().getDrawable(R.drawable.red_light);
 					}
 					
 					OverlayItem a = new OverlayItem(new GeoPoint(lat, lon), title,info);
 					
 					apOverlay = new AccessPointItemizedOverlay(drawable,this);
 					apOverlay.addOverlay(new OverlayItem(new GeoPoint(lat, lon), title,info));
 					mapOverlays.add(apOverlay);
 				}
 			
 			} while (cursor.moveToNext());
 		} 
 		finally {
 			cursor.close();
 		}	
	    
	    
    }
    
	protected void onDestroy() 
	{
		db.close();
	    super.onDestroy();
	}

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

    
    
    
    /*
     * Overlayy stuff
     */
    private class AccessPointItemizedOverlay extends ItemizedOverlay<OverlayItem>
    {
    	MapView Map;
    	private ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
    	private Context mContext;

    	public AccessPointItemizedOverlay(Drawable defaultMarker, Context context)
    	{
    	 	super(boundCenterBottom(defaultMarker));
     		mContext = context;	
     		populate();
     	}
    	

    	public void addOverlay(OverlayItem overlay)
    	{
    		items.add(overlay);
    		populate();
    	}
    	@Override
    	protected OverlayItem createItem(int i)
    	{
    		return items.get(i);
    	}
    	@Override
    	public int size()
    	{
    		return items.size();
    	}
     
    	@Override
    	protected boolean onTap(int index) {
    		OverlayItem item = items.get(index);
    		AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
    		dialog.setTitle(item.getTitle());
    		dialog.setMessage(item.getSnippet());
    		dialog.show();
    		return true;
    	}
    }

}
